import { Injectable } from '@nestjs/common'
import { HttpModuleOptionsFactory, HttpModuleOptions } from '@nestjs/common'
import { ConfigService } from './config.service'

@Injectable()
export class HttpConfigService implements HttpModuleOptionsFactory {
    // 注入config service取得env变量
    constructor(private readonly configService: ConfigService) {} // 就是回传TypeOrmOptions对象
    createHttpOptions(): HttpModuleOptions {
        console.log(this.configService.getBoolean('HTTP_CHANGE_ORIGIN'))

        return {
            timeout: this.configService.getNumber('HTTP_TIMEOUT'),
            maxRedirects: this.configService.getNumber('HTTP_MAX_REDIRECTS'),
            maxContentLength: this.configService.getNumber(
                'HTTP_MAX_CONTENT_LENGTH'
            ),
            withCredentials: this.configService.getBoolean(
                'HTTP_WITH_CREDENTIALS'
            ),
            baseURL: this.configService.get('HTTP_BASEURL')
            // changeOrigin: this.configService.getBoolean('HTTP_CHANGE_ORIGIN')
        }
    }
}
